home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- #include <ctype.h>
- #include "parse.h"
- #include "string.h"
-
- char texttoken[200];
- long textptr;
-
- float floattoken;
- long longtoken;
- long linecount = 1;
-
- char *fakefile = 0;
- char *fakefileptr;
-
- void parseerror(char *s)
- {
- char ss[120];
- sprintf(ss, "Line %d: %s\n", linecount, s);
- displaymessage(ss);
- }
-
- void parseerror(char *s, long line)
- {
- char ss[120];
- sprintf(ss, "Line %d: %s\n", line, s);
- displaymessage(ss);
- }
-
- void initfakefile(char *foo)
- {
- fakefile = fakefileptr = foo;
- }
-
- int tomsgetc(FILE *fp)
- {
- if (fp)
- return getc(fp);
- if (*fakefileptr)
- return (int)*fakefileptr++;
- return EOF;
- }
-
- void tomsungetc(int c, FILE *fp)
- {
- if (fp)
- ungetc(c, fp);
- else
- fakefileptr--;
- }
-
- static void readidstuff(FILE *fp, char *s)
- {
- char c;
- while(1) {
- c = tomsgetc(fp);
- if (isalnum(c) || c == '_' || c == '.')
- *s++ = c;
- else {
- *s = 0;
- if (c != EOF) tomsungetc(c, fp);
- return;
- }
- }
- }
-
- static void readreservedword(FILE *fp, char c)
- {
- texttoken[0] = c;
- readidstuff(fp, &texttoken[1]);
- }
-
- // The code that inserts a file simply sets the prefix to some
- // non-empty string for file insertion. This avoids (somewhat)
- // the problem with identifier conflicts. I decided a postfix
- // was better.
-
- extern char *suffix;
-
- static void readidentifier(FILE *fp, char c)
- {
- texttoken[0] = c;
- readidstuff(fp, &texttoken[1]);
- if (suffix) {
- strcat(texttoken, suffix);
- }
- }
-
- static tokentype readnumber(FILE *fp, char c)
- {
- char numstr[200], *nptr;
- long isint = 1;
- nptr = &numstr[0];
- do {
- if (c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E')
- isint = 0;
- *nptr++ = c;
- c = tomsgetc(fp);
- } while ( isdigit(c) || c == '.' || c == '-'
- || c == '+' || c == 'e' || c == 'E');
- if (c != EOF) tomsungetc(c, fp);
- *nptr = 0;
- if (isint) {
- sscanf(numstr, "%d", &longtoken);
- return Integer;
- } else {
- sscanf(numstr, "%g", &floattoken);
- return Float;
- }
- }
-
- tokentype handledot(FILE *fp)
- {
- char c = tomsgetc(fp);
- if (isdigit(c)) {
- tomsungetc(c, fp);
- return readnumber(fp, '.');
- }
- tomsungetc(c, fp);
- readreservedword(fp, '.');
- return Reserved;
- }
-
- static long readString(FILE *fp)
- {
- char *s = texttoken;
- char c;
-
- while (1) {
- c = tomsgetc(fp);
- if (c == '"') { *s = 0; return 1; }
- if (c == '\n' || c == EOF) {
- parseerror("Error: newline or EOF in string.");
- *texttoken = 0;
- return 0;
- }
- *s++ = c;
- }
- }
-
- tokentype gettoken(FILE *fp)
- {
- int c;
-
- // skip white space
- while (1) {
- c = tomsgetc(fp);
- if (c == '\n') linecount++;
- if (c == EOF) return Eof;
- if (c == '/') {
- if (tomsgetc(fp) != '/') {
- parseerror("Bad comment");
- return Error;
- }
- textptr = 0;
- while (1) {
- texttoken[textptr++] = c = tomsgetc(fp);
- texttoken[textptr] = (char)0;
- if (c == '\n') {linecount++; return Comment;}
- if (c == EOF) return Comment;
- }
- }
- if (isspace(c) == 0) break;
- }
- switch (c) {
- case ',':
- return Comma;
- case ';':
- return Semicolon;
- case '=':
- return Equal;
- case '(':
- return Open;
- case ')':
- return Close;
- case '{':
- return OpenCurly;
- case '}':
- return CloseCurly;
- case '"':
- if (readString(fp))
- return Str;
- return Error;
- case '.':
- return handledot(fp);
- default:
- if (isdigit(c) || c == '-')
- return(readnumber(fp, c));
- if (isalpha(c)) {
- readidentifier(fp, c);
- return Identifier;
- }
- parseerror("Garbage character");
- return Error;
- }
- }
-